package cn.imnu.legalaid.controller;

import cn.imnu.legalaid.dto.Lawyer.LawyerDetailDTO;
import cn.imnu.legalaid.dto.Lawyer.LawyerListDTO;
import cn.imnu.legalaid.dto.Lawyer.LawyerSearchDTO;
import cn.imnu.legalaid.service.LawyerProfilesService;
import cn.imnu.legalaid.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lawyers")
public class LawyerController {

    @Autowired
    private LawyerProfilesService lawyerProfilesService;

    /**
     * 分页查询律师列表
     */
    @PostMapping("/list")
    public Result getLawyerList(@RequestBody LawyerSearchDTO searchDTO) {
        try {
            IPage<LawyerListDTO> result = lawyerProfilesService.getLawyerList(searchDTO);
            
            Map<String, Object> data = new HashMap<>();
            data.put("records", result.getRecords());
            data.put("total", result.getTotal());
            data.put("current", result.getCurrent());
            data.put("size", result.getSize());
            data.put("pages", result.getPages());
            
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询律师列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有专业领域
     */
    @GetMapping("/specialties")
    public Result getAllSpecialties() {
        try {
            List<String> specialties = lawyerProfilesService.getAllSpecialties();
            return Result.success(specialties);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取专业领域失败: " + e.getMessage());
        }
    }

    /**
     * 获取律师详情
     */
    @GetMapping("/{id}")
    public Result getLawyerDetail(@PathVariable Integer id) {
        try {
            LawyerDetailDTO lawyerDetail = lawyerProfilesService.getLawyerDetailByProfileId(id);

            if (lawyerDetail == null) {
                return Result.error("未找到该律师信息");
            }

            return Result.success(lawyerDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取律师详情失败: " + e.getMessage());
        }

    }

    /**
     * 开始咨询律师
     */
    @PostMapping("/{id}/consult")
    public Result consultLawyer(@PathVariable Integer id) {
        try {
            // 这里可以实现开始咨询的逻辑
            return Result.success("开始咨询律师，ID: " + id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("开始咨询失败: " + e.getMessage());
        }
    }
}
